package com.pool;


import java.security.AccessController;
import java.util.concurrent.*;

// Executors 工具类：三大方法
public class Demo01 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程
//        ExecutorService threadPool = Executors.newFixedThreadPool(5); // 创建一个固定大小的线程池
//        ExecutorService threadPool = Executors.newCachedThreadPool();   // 可伸缩的，遇强则强，遇弱则弱

        try {
            for (int i = 1; i <= 100; i++) {
                // 使用了线程池之后，则利用线程池创建线程
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "=>OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 线程池用完，关闭线程池
            threadPool.shutdown();
        }
    }

    // 源码：七大参数
//    public ThreadPoolExecutor(int corePoolSize,                       // 核心线程池大小
//                              int maximumPoolSize,                    // 最大核心线程池大小
//                              long keepAliveTime,                     // 超时了没有人调用就会释放
//                              TimeUnit unit,                          // 超时单位
//                              BlockingQueue<Runnable> workQueue,      // 阻塞队列
//                              ThreadFactory threadFactory,            // 线程工厂：创建线程的，一般不用动
//                              RejectedExecutionHandler handler) {     // 拒绝策略
//        if (corePoolSize < 0 ||
//                maximumPoolSize <= 0 ||
//                maximumPoolSize < corePoolSize ||
//                keepAliveTime < 0)
//            throw new IllegalArgumentException();
//        if (workQueue == null || threadFactory == null || handler == null)
//            throw new NullPointerException();
//        this.acc = System.getSecurityManager() == null ?
//                null :
//                AccessController.getContext();
//        this.corePoolSize = corePoolSize;
//        this.maximumPoolSize = maximumPoolSize;
//        this.workQueue = workQueue;
//        this.keepAliveTime = unit.toNanos(keepAliveTime);
//        this.threadFactory = threadFactory;
//        this.handler = handler;
//    }
}
